﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScoreMS.Model;

namespace ScoreMS.BLL
{
    public class ClassBS
    {
        static public Class GetClass(string class_id)
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                Class model = new Class();
                var @class = db.@class.FirstOrDefault(x => x.class_id == class_id);
                model.class_id = @class.class_id;
                model.class_grade = @class.class_grade;
                model.class_institute_id = @class.class_institute_id;
                return model;
            }
        }

        /// <summary>
        /// 获取所有班级列表
        /// </summary>
        /// <returns></returns>
        static public List<Class> GetClassList()
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                var ret = db.@class;
                List<Class> list = new List<Class>();
                foreach (var c in ret)
                {
                    Class model = new Class();
                    model.class_id = c.class_id;
                    model.class_grade = c.class_grade;
                    model.class_institute_id = c.class_institute_id;
                    list.Add(model);
                }
                return list;
            }
        }

        /// <summary>
        /// 获取开指定课的班级列表
        /// </summary>
        /// <param name="course_id">课程ID</param>
        /// <returns></returns>
        static public List<Class> GetClassList(string course_id)
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                var ret = db.@class.Where(x => x.course.Any(c => c.course_id == course_id));
                List<Class> list = new List<Class>();
                foreach (var c in ret)
                {
                    Class model = new Class();
                    model.class_id = c.class_id;
                    model.class_grade = c.class_grade;
                    model.class_institute_id = c.class_institute_id;
                    list.Add(model);
                }
                return list;
            }
        }

        /// <summary>
        /// 添加班级
        /// </summary>
        /// <param name="student"></param>
        /// <returns></returns>
        static public bool AddClass(Class model)
        {
            try
            {
                using (scoremsdbEntities db = new scoremsdbEntities())
                {
                    db.@class.AddObject(new @class()
                    {
                        class_id = model.class_id,
                        class_grade = model.class_grade,
                        class_institute_id = model.class_institute_id,
                    });
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
